package apps.site.model;

import java.util.List;
import java.util.Map;

import com.xyrj.core.HHPojo;
import com.xyrj.mvc.HHModel;
import com.xyrj.utils.HHPojoUtils;

public abstract class HHBaseModel extends HHModel{
	
	/**
	 * 构造函数
	 * 
	 * 初始化类里成员
	 */
	public HHBaseModel()
	{
		super();
	}

	/**
	 * 得到主页显示的信息记录列表
	 *  
	 * @description
	 * 
	 * @title  getIndexCaseList
	 * @return List<Map> 得到的记录结果集
	 */
	@SuppressWarnings("rawtypes")
    public List<Map> getIndexList()
	{
		this._sql.table(this._pojo.table)
		.fields(new String[]{this._pojo.modelEnName+"_id",this._pojo.modelEnName+"_name","image_path","description"})
		.orderBy(HHPojoUtils.getOrderByFields(this._pojo))
		.where("pass_flag = 3")
		.limit(5);
		
		return this._db.find(this._sql.getSelectSql());
	}
	
	/**
	 * 得到当前查询的记录列表
	 *  
	 * @description
	 * 
	 * @title  getList
	 * @param  page 当前访问的页数
	 * @param  perpage 每一页需要显示的记录条数
	 * @return List<Map> 查询到的记录集
	 */
	@SuppressWarnings("rawtypes")
    public List<Map> getList(int page, int perpage)
	{
		this._sql.table(this._pojo.table)
		.fields(new String [] {this._pojo.modelEnName+"_id", this._pojo.modelEnName+"_name", "image_path", "description"})
		.where("`pass_flag`=3")
		.orderBy(HHPojoUtils.getOrderByFields(this._pojo))
		.limit(page, perpage);
		
		return this._db.find(this._sql.getSelectSql());
	}
	/**
	 * 通过ID得到对应的记录信息
	 *  
	 * @description
	 * 
	 * @title  getRecordById
	 * @param  int id 当前的记录ID
	 * @return Map 查到的记录信息
	 */
	@SuppressWarnings("rawtypes")
    public Map getRecordById(int id)
	{
		this._sql.table(this._pojo.table)
		.fields("*")
		.where(new String [] {"`"+this._pojo.modelEnName+"_id`=" + id, "AND `pass_flag`=3"})
		.limit(1);
		List<Map> list 	= this._db.find(this._sql.getSelectSql());
		if(null == list || list.size() < 1) { 
			return null;
		}
	
		return list.get(0);
	}

	/**
	 * 得到比当前记录ID小的记录
	 *  
	 * @description
	 * 
	 * @title  getLowCurrentIdCasesList
	 * @param  int id 当前访问的记录ID
	 * @return List<Map> 得到查询后的记录结果集
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> getLowCurrentIdList(int id)
	{
		return this._getListByWhere("`"+this._pojo.modelEnName+"_id` > " + id);
	}

	/**
	 * 得到比当前大的前五条记录
	 *  
	 * @description
	 * 
	 * @title  getBigCurrentIdCasesList
	 * @param  int id 当前访问的记录ID
	 * @return List<Map> 得到查询后的记录结果集
	 */
	@SuppressWarnings("rawtypes")
	public List<Map> getBigCurrentIdList(int id)
	{
		return this._getListByWhere("`"+this._pojo.modelEnName+"_id` < " + id);
	}

	/**
	 * 通过给定的条件来得到当前的记录列表 
	 *  
	 * @description
	 * 
	 * @title  _getListByWhere
	 * @param  String where 条件
	 * @return List<Map> 满足条件的列表
	 */
	@SuppressWarnings("rawtypes")
	protected List<Map> _getListByWhere(String where)
	{
		this._sql.table(this._pojo.table)
		.fields(new String [] { this._pojo.modelEnName+"_id", this._pojo.modelEnName+"_name", "image_path"})
		.where(new String [] {where, "AND `pass_flag` = 3"})
		.orderBy(HHPojoUtils.getOrderByFields(this._pojo))
		.limit(5);
		
		return this._db.find(this._sql.getSelectSql());
	}
	
	/**
	 * 根据主键ID取到上一条记录
	 * @Title: getLaseRecodeById 
	 * @Description:  
	 * @param @param id
	 * @param @return  
	 * @return Map  
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public Map getLastRecodeById(int id) {
		this._sql.table(this._pojo.table)
		.fields(new String[]{this._pojo.modelEnName+"_id",this._pojo.modelEnName+"_name"})
		.where("`"+this._pojo.modelEnName+"_id` <" + id)
		.orderBy("`"+this._pojo.modelEnName+"_id`")
		.limit(1);
		List<Map> list =this._db.find(this._sql.getSelectSql());
		if(null == list || list.size()<1){
			return null;
		}
		return list.get(0);
	}
	/**
	 * 根据主见ID取到下一条记录
	 * @Title: getNextRecodeById 
	 * @Description:  
	 * @param @param id
	 * @param @return  
	 * @return Map  
	 * @throws
	 */
	@SuppressWarnings("rawtypes")
	public Map getNextRecodeById(int id) {
		this._sql.table(this._pojo.table)
		.fields(new String[]{this._pojo.modelEnName+"_id",this._pojo.modelEnName+"_name"})
		.where("`"+this._pojo.modelEnName+"_id` >" + id)
		.orderBy("`"+this._pojo.modelEnName+"_id`")
		.limit(1);
		List<Map> list =this._db.find(this._sql.getSelectSql());
		if(null == list || list.size()<1){
			return null;
		}
		return list.get(0);
	}
	/**
	 * 添加留言信息
	 * @Title: add 
	 * @Description:  
	 * @param @return  
	 * @return int  
	 * @throws
	 */
	public int add(HHPojo pojo){
		try {
			return (Integer)this._db.savePojo(pojo);
		} catch(Exception ex) {
			return 0;
		}
	}
}
